﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using HuaweiCloud.Dotnet.Samples.ESports.Dbs;
using HuaweiCloud.Dotnet.Samples.ESports.Entities;
using HuaweiCloud.Dotnet.Samples.ESports.Services;

namespace HuaweiCloud.Dotnet.Samples.ESports.Pages.Steps
{
    public class DeleteModel : PageModel
    {
        private readonly IStepAppService _service;

        public DeleteModel(IStepAppService service)
        {
            _service = service;
        }

        [BindProperty]
      public Step Step { get; set; } = default!;

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var step = await _service.GetAsync(id.Value);

            if (step == null)
            {
                return NotFound();
            }
            else 
            {
                Step = step;
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            await _service.DeleteAsync(id.Value);
            return RedirectToPage("./Index");
        }
    }
}
